package Huihu.src.Duoxiancheng;

import java.io.*;

//练习Thread类，实现多线程同步下载图片
public class TestThreads extends Thread{
    @Override
    public void run() {
        BufferedInputStream bufferedInputStream= null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream("libs/a.jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedOutputStream bufferedOutputStream= null;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("libs/c.jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte [] bys=new byte[1024];
        while (true) {
            int len = 0;
            try {
                if (!((len = bufferedInputStream.read(bys)) != -1)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bufferedOutputStream.write(bys, 0, len);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            bufferedInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TestThreads testThreads=new TestThreads();
        testThreads.start();
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("libs/a.jpg"));
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("libs/b.jpg"));
        byte [] bys=new byte[1024];
        int len;
        while ((len = bufferedInputStream.read(bys)) != -1){
            bufferedOutputStream.write(bys,0,len);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}
